package mms.farina.thecell.activities;

import java.util.List;

import mms.farina.thecell.MapItemizedOverlay;
import mms.farina.thecell.R;
import mms.farina.thecell.services.ReceiveService;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**Activity per la visualizzazione delle mappe nella versione 1*/
public class Map_v1 extends MapActivity {

	List<Overlay> mapOverlays;
	MapItemizedOverlay itemizedoverlay=null;
	MapItemizedOverlay myPos=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map_v1);
		MapView mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    
	    mapOverlays = mapView.getOverlays();
	    
	    Drawable foreignIcon = this.getResources().getDrawable(R.drawable.greenflag);
	    Drawable myIcon = this.getResources().getDrawable(R.drawable.redflag);
	    
	    itemizedoverlay = new MapItemizedOverlay(foreignIcon, this);
	    myPos = new MapItemizedOverlay(myIcon, this);
	    updateOverlay();
	    
	}
	
	
	private void updateOverlay(){

		if(ReceiveService.geoInfo==null)return;
		for(String k: ReceiveService.geoInfo.keySet()){
			
			double[] coordinate=ReceiveService.geoInfo.get(k);
			
			int lat=(int)(coordinate[0]*1e6);
			int lon=(int) (coordinate[1]*1e6);
			
			GeoPoint point = new GeoPoint(lat,lon);
		    OverlayItem overlayitem = new OverlayItem(point, k, "Posizione Corrente");
		    if(k.equals(ReceiveService.NAME))myPos.addOverlay(overlayitem);
		    else itemizedoverlay.addOverlay(overlayitem);
		    
		}
		if(myPos.size()>0)mapOverlays.add(myPos);
		if(itemizedoverlay.size()>0)mapOverlays.add(itemizedoverlay);
		
	}
	
	public void refreshButtonListener(View v){
		updateOverlay();
	}
	
	public void chageViewListener(View v){

		RadioGroup rbg=(RadioGroup) findViewById(R.id.mapGroup2);
		RadioButton rb=(RadioButton) findViewById(rbg.getCheckedRadioButtonId());
		MapView mapView = (MapView) findViewById(R.id.mapview);
		
		
		
		if(rb.getText().equals("Simple"))mapView.setSatellite(false);
		else mapView.setSatellite(true);
		
	}


	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	protected void onStop(){
		super.onStop();
    	finish();
	}

	@Override
	protected void onPause(){
		super.onPause();
    	finish();
	}

}
